<template>
  <Dialog v-model="dialogVisible" title="选择关键词组" width="500" align-center>
    <el-form
      @submit.prevent
      label-width="140px"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      ref="formRef"
    >
      <el-form-item label="上级关键词组" prop="parentId">
        <el-cascader
          style="width: 100%"
          v-model.trim="formData.parentId"
          :options="ketWordTree"
          :props="cascaderProps"
          clearable
          placeholder="请选择上级关键词组"
          maxlength="50"
        />
      </el-form-item>
      <el-form-item label="关键词组" prop="keywordType">
        <el-input
          style="width: 100%"
          v-model.trim="formData.keywordType"
          clearable
          placeholder="请输入关键词组"
          maxlength="20"
        />
      </el-form-item>
      <el-form-item label="结构化类型：" prop="type">
        <el-select v-model="formData.type" clearable placeholder="请选择标准类型">
          <el-option label="非机构化数据" value="0" />
          <el-option label="结构化数据" value="1" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submit" :disabled="formLoading">确定</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { getKeywordTree } from '@/api/develop/featureLibrary/keyword'

interface RestaurantItem {
  value: string
}
const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false)
const ketWordTree = ref([])
const formRef = ref<FormInstance>()
const formData = ref({
  type: '',
  keywordType: '',
  parentId: ''
})
const cascaderProps = ref({
  checkStrictly: true,
  label: 'keywordType',
  value: 'id',
  children: 'children',
  emitPath: false
})
const formRules = reactive({
  keywordType: [{ required: true, message: '关键词组不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '结构化类型不能为空', trigger: 'change' }]
})

const getKeyWordTree = async () => {
  const data = await getKeywordTree()
  ketWordTree.value = data || []
}

const createFilter = (queryString: string) => {
  return (restaurant: RestaurantItem) => {
    return restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
  }
}

const restaurants = computed<RestaurantItem[]>(() => {
  return keywordTypeList.value.map((item) => ({
    value: item
  }))
})

const keywordTypeList = ref()

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  getKeyWordTree()
}

async function submit() {
  await formRef.value?.validate()
  formData.value.parentId = formData.value.parentId ? formData.value.parentId : '0'
  const data = toRaw(formData.value)
  emit('success', data)
}

defineExpose({ open, close: closeDialog, setBtnStatus }) // 提供 open 方法，用于打开弹窗

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

function closeDialog() {
  dialogVisible.value = false
}

function setBtnStatus(status: boolean) {
  formLoading.value = status
}

/** 重置表单 */
const resetForm = () => {
  formRef.value?.resetFields()
}

watch(dialogVisible, (show) => {
  !show && resetForm()
})
</script>
<style lang="scss" scoped></style>
